package com.tocean.demo.service;

import com.tocean.demo.dto.ExaminationSet;
import com.tocean.demo.entity.questionbank.HisTestRecord;
import com.tocean.demo.entity.questionbank.HisTestScore;
import com.tocean.demo.repository.HisTestScoreRepository;
import com.tocean.demo.util.SnowflakeIdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class HisTestScoreService {

    @Autowired
    private SnowflakeIdWorker snowflakeIdWorker;

    @Autowired
    private HisTestScoreRepository hisTestScoreRepository;

    @Autowired
    private HisTestRecordService hisTestRecordService;

    /**
     * 添加历史成绩
     * @param examinationSet
     */
    public void AddScore( ExaminationSet examinationSet){
        List<HisTestScore> hisTestScore =  hisTestScoreRepository.findAllByUserId(examinationSet.getUserId());

        if(hisTestScore.size() == 0){
            HisTestScore hisScore = new HisTestScore();
            hisScore.setId(snowflakeIdWorker.nextId());
            hisScore.setUserId(examinationSet.getUserId());
            hisScore.setScore(examinationSet.getMyQuestions().get(0).getScore());
            hisScore.setType(examinationSet.getType());
            hisScore.setUsingTime(examinationSet.getUsingTime());
            hisScore.setCommitTime(examinationSet.getCommitTime());
            hisScore.setTag_Name(examinationSet.getMyQuestions().get(0).getTag_Name());

            hisScore.setSequenceId("1");
            hisTestScoreRepository.save(hisScore);

            hisTestRecordService.AddTestRecord(examinationSet,"1");
        }else if(hisTestScore.size() != 0 ){
            int temper = hisTestScore.size() + 1;
            String temp = String.valueOf(temper);

            HisTestScore hisScore = new HisTestScore();
            hisScore.setId(snowflakeIdWorker.nextId());
            hisScore.setUserId(examinationSet.getUserId());
            hisScore.setScore(examinationSet.getMyQuestions().get(0).getScore());
            hisScore.setType(examinationSet.getType());
            hisScore.setUsingTime(examinationSet.getUsingTime());
            hisScore.setCommitTime(examinationSet.getCommitTime());
            hisScore.setTag_Name(examinationSet.getMyQuestions().get(0).getTag_Name());

            hisScore.setSequenceId(temp);
            hisTestScoreRepository.save(hisScore);

            hisTestRecordService.AddTestRecord(examinationSet,temp);
        }
    }

    /**
     * 查找所有的历史成绩
     *
     */
    public List<HisTestScore> findAllScores(String userId){
        List<HisTestScore> mid = hisTestScoreRepository.findAllByUserId(userId);
        List<HisTestScore> hisTestScores = new ArrayList<>();

        for(int i=0;i<mid.size();i++){
            int m = i + 1;
            for(int k = 0 ; k < mid.size() ; k++){
                if(mid.get(k).getSequenceId().equals(String.valueOf(m))){
                    hisTestScores.add(mid.get(k));
                    break;
                }
            }
        }

        return hisTestScores;
    }
}
